#include "pool_head.h"

int do_work(int local_fd){
    while(1){
        int new_fd,flag;
        recv_message(local_fd,&new_fd,&flag);
        if(flag == -1){
            //说明子进程要退出
            printf("子进程退出...\n");
            exit(0);
        }
        translate_to_client(new_fd);
        close(new_fd);
        //向父进程发送一条消息，表示自己当前空闲
        send(local_fd,"1",1,0);
    }
    return 0;
}

int translate_to_client(int client_fd){
    // send(client_fd,"client",6,0);
    data_t msg_data;
    memset(&msg_data,0,sizeof(msg_data));
    recv(client_fd,&msg_data.len,sizeof(int),MSG_WAITALL);
    printf("cur_msg len : %d\n",msg_data.len);
    //读取传输方式
    recv(client_fd,msg_data.msg,msg_data.len,MSG_WAITALL);
    printf("cur_msg : %s\n",msg_data.msg);

    data_t file_msg;
    memset(&file_msg,0,sizeof(file_msg));
    //读取文件名长度
    recv(client_fd,&file_msg.len,sizeof(int),MSG_WAITALL);
    printf("file_name len : %d\n",file_msg.len);
    //读取文件名
    recv(client_fd,file_msg.msg,file_msg.len,MSG_WAITALL);
    printf("file_name : %s\n",file_msg.msg);
    if(strcmp(msg_data.msg,"download") == 0){
        download(client_fd,file_msg.msg);
    }else if(strcmp(msg_data.msg,"upload") == 0){
        upload(client_fd,file_msg.msg);
    }
    return 0;
}